package com.qingxin.credit.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.credit.entity.Credit;
import com.qingxin.credit.service.CreditService;
import com.qingxin.park.entity.Company;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;

@Controller
@RequestMapping(value="/credit")
public class CreditAction extends BaseAction{
	@Autowired
	private CreditService creditService;
	
	/**
	 * 跳转列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do",method=RequestMethod.GET)
	public String list(ModelMap model,HttpServletRequest request){
		return "trace/credit/credit_list";
	}
	
	/**
	 * 列表数据
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search, PageParam pageParam, HttpServletRequest request){
		Pager pager = creditService.list(pageParam,search, request);
		return pager;
	}
	
	/**
	 * 添加
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "/trace/credit/credit_add";
	}
	
	/**
	 * 保存添加
	 * @param credit
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Object save(@Valid Credit credit,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = creditService.add(credit,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 被评定主体信息（企业）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCompanyData", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<Company> getParkData(HttpServletRequest request){
		return creditService.getParkData(getLoginUser(request));
	}
	
	/**
	 * 删除
	 * @param creditid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] creditid, HttpServletRequest request){
		if(creditid==null ){
			return new SysError("没有选择任何信息.");
		}
		return creditService.delete(creditid, request);
	}
	
	/**
	 * 修改
	 * @param model
	 * @param creditid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String creditid, HttpServletRequest request){
		Credit credit = creditService.get(creditid);
		model.put("credit", credit);
		return "trace/credit/credit_edit";
	}
	
	/**
	 * 保存修改
	 * @param credit
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid Credit credit, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return creditService.update(credit, getLoginUser(request), request);
	}
	
}
